//
//  NewsViewController.m
//  ZhongKeXinHeater
//
//  Created by Kendama on 2017/6/1.
//  Copyright © 2017年 DT. All rights reserved.
//

#import "NewsViewController.h"
#import "PublicGetMessageListApi.h"
#import "NewsModel.h"
#import "PublicDeleteMassageApi.h"

@interface NewsViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *dataSource;
/** 空数据提示 */
@property (nonatomic,strong) UILabel *emptyLabel;

@end

@implementation NewsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)refreshBaseControlValue
{
    
    [self setNavigationTitle:@"消息" titleColor:[UIColor blackColor]];
    self.navigationBar.backgroundColor = [UIColor colorWithHexString:@"D0D0D0"];
    
    [self masLayoutSubviews];
    
    [self.tableView.mj_header beginRefreshing];
}

#pragma mark -- 事件
- (void)getNewsListWithPage:(NSInteger)page {
    PublicGetMessageListApi *api = [[PublicGetMessageListApi alloc]initWithDic:@{@"device_type":@"0",@"page":[NSString stringWithFormat:@"%ld",(long)page]}];
    [api dtDefaultInterceptStartYTKRequestCompletionBlockWithSuccess:^(YTKBaseRequest * _Nonnull request) {
        [self.dataSource removeAllObjects];
        for (NSDictionary *dic in [request responseJSONObject][@"data"]) {
            
            NewsModel *model = [NewsModel yy_modelWithDictionary:dic];
            
            [self.dataSource addObject:model];
        }
        
        if (self.dataSource.count == 0) {
            self.emptyLabel.hidden = NO;

        } else {
            self.emptyLabel.hidden = YES;
        }
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
    } failure:^(YTKBaseRequest * _Nonnull request) {
        if ( [request.responseJSONObject[@"errmsg"] isEqualToString:@"没有数据"] || [request.responseJSONObject[@"errmsg"] isEqualToString:@"在限定时间内重复请求"]) {
            self.emptyLabel.hidden = NO;
        }else{
            self.emptyLabel.hidden = YES;
        }
        
        if (self.dataSource.count == 0) {
            self.emptyLabel.hidden = NO;
            
        } else {
            self.emptyLabel.hidden = YES;
            
        }
        [self.tableView.mj_header endRefreshing];

    }];
    
}

#pragma mark -- 代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataSource.count;
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return px1920Height(350);
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return px1920Height(50);
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NewsTableViewCell *cell = [NewsTableViewCell cellWithTableView:tableView];
    NewsModel *news = self.dataSource[indexPath.section];
    cell.newsModel = news;

    return cell;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}

- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 添加一个删除按钮
    NSMutableArray *array = [[NSMutableArray alloc]init];
    
    UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        
        [tableView reloadData];
        
        [[AlertViewManager shareManager]initWithMessage:@"是否确认删除该消息？" cancleButtonTitle:@"取消" OtherButtonsArray:@[@"确认"] clickAtIndex:^(NSInteger buttonIndex, UIAlertView *alertView) {
            if (buttonIndex == 1) {
                NewsModel *news = self.dataSource[indexPath.section];
                PublicDeleteMassageApi *api = [[PublicDeleteMassageApi alloc]initWithDic:@{@"id":news.ids}];
                [api dtDefaultInterceptStartYTKRequestCompletionBlockWithSuccess:^(YTKBaseRequest * _Nonnull request) {
                    [self.dataSource removeObject:news];
                    [SVProgressHUD showInfoWithStatus:request.responseObject[@"errmsg"]];
                    [self getNewsListWithPage:1];
                    
                } failure:^(YTKBaseRequest * _Nonnull request) {
                    NSLog(@"错误：%@",request.responseObject[@"errmsg"]);
                }];
            }
        }];
    }];
    
    [array addObject:deleteRowAction];
    
    return array;
}


- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
}



#pragma mark -- 懒加载
- (UITableView *)tableView
{
    if(!_tableView)
    {
        UIView *superView = self.view;
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [self getNewsListWithPage:1];
        }];
        [superView addSubview:_tableView];
    }
    return _tableView;
}


- (NSMutableArray *)dataSource{
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

- (UILabel *)emptyLabel{
    
    if (!_emptyLabel) {
        _emptyLabel = [[UILabel alloc]init];
        _emptyLabel.text = @"暂无消息通知";
        _emptyLabel.font = [UIFont systemFontOfSize:px1080Width(42)];
        _emptyLabel.textColor = [UIColor colorWithRed:151/255.0 green:151/255.0 blue:151/255.0 alpha:1];
        [self.view addSubview:_emptyLabel];
    }
    return _emptyLabel;
}

#pragma mark -- 初始化
- (void) masLayoutSubviews{
    __weak UIView *superView = self.view;
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(superView).with.offset(64);
        make.left.right.equalTo(superView);
        make.bottom.equalTo(superView).with.offset(-49);
    }];
    
    [self.emptyLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.centerY.equalTo(self.view);
    }];
    
    self.emptyLabel.hidden = YES;
}

@end
